package Ants.Modules.Fight.Minimax.Model;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import Ants.Model.Status;

public class Tile {
	

	public Tile(int r, int c) {
		this(r,c,false);
	}
	
	public Tile(int r, int c, boolean frozen){
		this.row = r;
		this.col = c;
		this.frozen = frozen;
	}
	
	private int row;
	private int col;
	private boolean frozen;

	public List<Tile> getMoves(){
		
		int MAP_ROWS = Status.Rows;
		int MAP_COLS = Status.Cols;
		
		List<Tile> list = new LinkedList<Tile>();
		
		if(!frozen){
			//n
			list.add(new Tile((row+MAP_ROWS-1)%MAP_ROWS,col));
			//e
			list.add(new Tile(row,(col+1)%MAP_COLS));
			//s
			list.add(new Tile((row+1)%MAP_ROWS,col));
			//w
			list.add(new Tile(row,(col+MAP_COLS-1)%MAP_COLS));
		}

		//stay
		list.add(new Tile(row,col));
	
		Collections.shuffle(list, new Random(System.currentTimeMillis()));
		
		return list;
	}
	
	public List<Tile> getMovesWhitoutStay(){
		
		int MAP_ROWS = Status.Rows;
		int MAP_COLS = Status.Cols;
		
		List<Tile> list = new LinkedList<Tile>();
		//n
		list.add(new Tile((row+MAP_ROWS-1)%MAP_ROWS,col));
		//e
		list.add(new Tile(row,(col+1)%MAP_COLS));
		//s
		list.add(new Tile((row+1)%MAP_ROWS,col));
		//w
		list.add(new Tile(row,(col+MAP_COLS-1)%MAP_COLS));

		return list;
	}
	
	@Override
	public String toString() {
		return "R"+row+" C"+col;
	}

	public int getRow() {
		return row;
	}

	public int getCol() {
		return col;
	}
	
	@Override
	public boolean equals(Object obj) {
		  if (obj == null) return false;
		  if (obj == this) return true;
		  if (!(obj instanceof Tile))return false;
		  Tile other = (Tile)obj;
		  return other.row == row && other.col == col;
	}
	
	@Override
	public int hashCode() {
		 int hash = 1;
        hash = hash * 17 + row;
        hash = hash * 31 + col;
        return hash;
	}
	
	public static int GetDistRow(Tile a,Tile b){
		int deltaRow = Math.abs(a.getRow() - b.getRow());
		return Math.min(deltaRow,Status.Rows - deltaRow);
	}

	public static int GetDistCol(Tile a,Tile b){
		int deltaCol = Math.abs(a.getCol() - b.getCol());
		return Math.min(deltaCol,Status.Cols - deltaCol);
	}
	
}
